/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.oyun.modules.medicine.domain.MnPrescription;
import com.oyun.modules.medicine.domain.Pharmacopeia;

import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.service.PharmacopeiaService;
import com.oyun.modules.medicine.domain.vo.PharmacopeiaQueryCriteria;
import com.oyun.modules.medicine.mapper.PharmacopeiaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;


/**
* @description 服务实现
* @author 呼和
* @date 2024-07-01
**/
@Service
@RequiredArgsConstructor
public class PharmacopeiaServiceImpl extends ServiceImpl<PharmacopeiaMapper, Pharmacopeia> implements PharmacopeiaService {

    private final PharmacopeiaMapper pharmacopeiaMapper;

    @Override
    public PageResult<Pharmacopeia> queryAll(PharmacopeiaQueryCriteria criteria, Page<Object> page){
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        return PageUtil.toPage(pharmacopeiaMapper.findAll(criteria, page));
    }

    @Override
    public List<Pharmacopeia> queryAll(PharmacopeiaQueryCriteria criteria){
        return pharmacopeiaMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Pharmacopeia resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Pharmacopeia resources) {
        Pharmacopeia pharmacopeia = getById(resources.getId());
        pharmacopeia.copy(resources);
        convertCode(pharmacopeia);
        saveOrUpdate(pharmacopeia);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Pharmacopeia> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Pharmacopeia pharmacopeia : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药物名称", pharmacopeia.getMnName());
            map.put(" mnNameCode",  pharmacopeia.getMnNameCode());
            map.put(" zhName",  pharmacopeia.getZhName());
            map.put("蒙语说明", pharmacopeia.getMnInstructions());
            map.put(" zhInstructions",  pharmacopeia.getZhInstructions());
            map.put(" mnInstructionsCode",  pharmacopeia.getMnInstructionsCode());
            map.put(" createBy",  pharmacopeia.getCreateBy());
            map.put(" createTime",  pharmacopeia.getCreateTime());
            map.put(" updateBy",  pharmacopeia.getUpdateBy());
            map.put(" updateTime",  pharmacopeia.getUpdateTime());
            map.put(" status",  pharmacopeia.getStatus());
            map.put("备注", pharmacopeia.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    private void convertCode(Pharmacopeia pharmacopeia) {
        pharmacopeia.setMnNameCode(IntermediateCodeUtils.mn2Code(pharmacopeia.getMnName()).replaceAll("-", " -"));
    }
}
